//
//  MororcadePresentController.m
//  ICrEAM
//
//  Created by mac on 16/10/31.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "MororcadePresentController.h"
#import "TPKeyboardAvoidingTableView.h"
#import "PresentBottomCell.h"
#import "PresentTopTableViewCell.h"
#import "ProxyCadeAction.h"
#import "ProxyCadeRespose.h"
#import "CadeOperationAction.h"
#import "ChoseMenuViewController.h"
NSString * const MororcadeNotice=@"MororcadeNotice";
@interface MororcadePresentController ()<UITableViewDelegate>{
     //选择的中断维修代码
    NSString *_cancelCode;
    
}
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIView *lineView;
@property (weak, nonatomic) IBOutlet TPKeyboardAvoidingTableView *presentTable;
@property(nonatomic,strong)ProxyCadeRespose *proxyModel;
//中断维修原因数组
@property(nonatomic,strong)NSArray *cancelResonArr;

@end
//2126维修中断原因
@implementation MororcadePresentController
-(NSArray *)cancelResonArr{
    if (!_cancelResonArr) {
        _cancelResonArr=[ICrEAMTool getParameterArrayWithCode:@"2126"];
    }
    return _cancelResonArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    if(![ICrEAMTool isVW]){
        self.titleLabel.textColor=KSKBgColor;
        self.lineView.backgroundColor=KSKBgColor;
    }
    // 设置控制器在popover中的尺寸
    self.preferredContentSize = CGSizeMake(ScreenWidth-120, ScreenHeight-140);
    self.view.backgroundColor=BackgroundColor_Gray;
    [self requestProxyDetail];
    // Do any additional setup after loading the view from its nib.
}
#pragma mark 获取当前页面详情接口
-(void)requestProxyDetail{
    ProxyCadeAction *acation=[[ProxyCadeAction alloc]init];
    acation.ro_num=self.ro_num;
    [self requestDataWithAction:acation haveHud:YES showError:NO  success:^(__kindof YTKBaseRequest *request) {
        NSDictionary *dic=request.responseJSONObject;
        self.proxyModel=[ProxyCadeRespose mj_objectWithKeyValues:dic];
        if (dic[@"RPR_BREAK_REASON"]) {
            _cancelCode=[NSString stringWithFormat:@"%@",dic[@"RPR_BREAK_REASON"]] ;
        }
        
        [self.presentTable reloadData];
    } failure:^(__kindof YTKBaseRequest *request) {
         [self.presentTable reloadData];
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==0) {
        return 185;
    }
    return 90;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (!self.proxyModel) {
        return 0;
    }
    return 5;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==0) {
        PresentTopTableViewCell *topCell=[PresentTopTableViewCell createPresentTopTableViewCell];
        UIView *linew=[[UIView alloc]initWithFrame:CGRectMake(0, 184, ScreenWidth, 1)];
        linew.backgroundColor=RGB(220, 220, 220);
        [topCell.contentView addSubview:linew];
        if (self.proxyModel) {
            topCell.proxyL.text=self.ro_num;
            topCell.liseceL.text=self.proxyModel.license;
            topCell.vinL.text=self.proxyModel.vin;
            topCell.carTypeL.text=self.proxyModel.modelName;
            topCell.serviceNameL.text=self.proxyModel.serviceAdvisor;
            topCell.gzDetailL.text=self.proxyModel.roTroubleDesc;
            topCell.checkDetailL.text=self.proxyModel.checkResult;
        }
        return topCell;
    }else{
        PresentBottomCell *cell=[PresentBottomCell createPresentBottomCell];
        switch (self.repairType) {
            case RepairStartType:{
                cell.hideotherView=YES;
                cell.enableOprationBtn=YES;
                if (indexPath.row==2) {
                    cell.hideotherView=NO;
                    cell.enablecancelResonBtn=YES;
                }
                if (indexPath.row==3) {
                    cell.enableOprationBtn=NO;
                }
                
            } break;
            case RepairProcedType:
                cell.hideotherView=YES;
                cell.enableOprationBtn=NO;
                
                if (indexPath.row==2) {
                    cell.enableOprationBtn=YES;
                    cell.hideotherView=NO;
                    cell.enablecancelResonBtn=YES;
                }
                if (indexPath.row==4) {
                    cell.enableOprationBtn=YES;
                }
                break;
            case RepairCancelType:
                cell.hideotherView=YES;
                cell.enableOprationBtn=NO;
                
                if (indexPath.row==2) {
                    cell.hideotherView=NO;
                    cell.enablecancelResonBtn=NO;
                }
                if (indexPath.row==3) {
                    cell.enableOprationBtn=YES;
                }
                break;
            case RepairEndType:
                cell.hideotherView=YES;
                cell.enableOprationBtn=NO;
                
                if (indexPath.row==2) {
                    cell.hideotherView=NO;
                    cell.enablecancelResonBtn=NO;
                }
                break;
                
            default:
                break;
                
        }
        
        if (!self.isOperation) {
            cell.enableOprationBtn=NO;
            cell.enablecancelResonBtn=NO;
        }
        if (indexPath.row==1) {
            //开始维修
            [cell.oprationBtn setTitle:@"开始维修" forState:UIControlStateNormal];
            if (!IsStrEmpty(self.proxyModel.beginWorkTime)) {
                cell.repairTime.text=self.proxyModel.beginWorkTime;
            }
#pragma mark 维修开始操作
            @weakify(self);
            cell.operationBlock=^(){
                @strongify(self);
                [self operationWithType:OperationStartType];
            };
            cell.repairLable.text=@"维修开始时间";
        }
        if (indexPath.row==2) {
            //中断维修
            [cell.oprationBtn setTitle:@"中断维修" forState:UIControlStateNormal];
            if (!IsStrEmpty(self.proxyModel.breakTime)) {
                cell.repairTime.text=self.proxyModel.breakTime;
            }
            if (!IsStrEmpty(self.proxyModel.repairBreakReason)) {
                [cell.cancelReson setTitle:self.proxyModel.repairBreakReason forState:UIControlStateNormal];
                [cell.cancelReson setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                
            }
            if (!IsStrEmpty(self.proxyModel.firstBreakTime)) {
                cell.FirstCancelLabel.text=self.proxyModel.firstBreakTime;
            }
            cell.repairLable.text=@"维修中断时间";
#pragma mark 维修中断操作
            @weakify(self);
            cell.operationBlock=^(){
                @strongify(self);
                if (self.repairType==RepairStartType) {
                    
                    [self alertMessage:@"是否中断维修？" withSuccessBlock:^{
                         [self operationWithType:OperationCancelType];
                    }];
            } else{
                [self operationWithType:OperationCancelType];
                }
            };
            cell.cancelResonBlock=^(){
                @strongify(self);
                [self popCancelReson];
            };
        }
        if (indexPath.row==3) {
#pragma mark 维修恢复操作
            [cell.oprationBtn setTitle:@"恢复维修" forState:UIControlStateNormal];
            if (!IsStrEmpty(self.proxyModel.recoverTime)) {
                cell.repairTime.text=self.proxyModel.recoverTime;
            }
            cell.repairLable.text=@"维修恢复时间";
            @weakify(self);
            cell.operationBlock=^(){
                @strongify(self);
                [self operationWithType:OperationRepalyType];
            };
        }
        if (indexPath.row==4) {
            if (!IsStrEmpty(self.proxyModel.finishTime)) {
                cell.repairTime.text=self.proxyModel.finishTime;
            }
            [cell.oprationBtn setTitle:@"结束维修" forState:UIControlStateNormal];
            cell.repairLable.text=@"维修结束时间";
#pragma mark 结束维修操作
            @weakify(self);
            cell.operationBlock=^(){
                @strongify(self);
                [self alertMessage:@"确认维修结束？" withSuccessBlock:^{
                     [self operationWithType:OperationEndType];
                }];
            };
        }
        
        return cell;
    }
}
#pragma mark 操作接口
-(void)operationWithType:(OperationType)opreationType{
    CadeOperationAction *action=[[CadeOperationAction alloc]init];
    action.operationType=opreationType;
    action.ro_num=self.ro_num;
    if (opreationType==OperationCancelType) {
        if (!IsStrEmpty(_cancelCode)) {
            action.breakReason=_cancelCode;
        }else{
            [self showText:@"请选择中断原因"];
            return;
        }
    }
    [self requestDataWithAction:action haveHud:YES showError:NO  success:^(__kindof YTKBaseRequest *request) {
        NSDictionary *respose=request.responseJSONObject;
        [[NSNotificationCenter defaultCenter]postNotificationName:MororcadeNotice object:nil];
        switch (opreationType) {
            case OperationStartType:
                self.proxyModel.beginWorkTime=respose[@"BEGIN_WORK_TIME"];
                self.repairType=RepairProcedType;
                break;
            case OperationCancelType:
                
                self.repairType=RepairCancelType;
                self.proxyModel.breakTime=respose[@"BREAK_TIME"];
                
                break;
            case OperationRepalyType:
                self.proxyModel.recoverTime=respose[@"RECOVER_TIME"];
                self.repairType=RepairProcedType;
                break;
            case OperationEndType:
                self.repairType=RepairEndType;
                self.proxyModel.finishTime=respose[@"APP_END_TIME"];
                break;
                
            default:
                break;
        }
        [self.presentTable reloadData];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1* NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self dismissViewControllerAnimated:YES completion:nil];
        });
        
    } failure:^(__kindof YTKBaseRequest *request) {
        
    }];
    
}
#pragma mark 中断原因弹框
-(void)popCancelReson{
    ChoseMenuViewController *choseMenu = [[ChoseMenuViewController alloc] init];
    PresentBottomCell *cell=[self.presentTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
    choseMenu.modalPresentationStyle = UIModalPresentationPopover;
    choseMenu.popoverPresentationController.sourceView = cell.cancelReson;
    choseMenu.popoverPresentationController.sourceRect =CGRectMake(0, 0,cell.cancelReson.width,cell.cancelReson.height);
    choseMenu.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp;
    CGFloat height =self.cancelResonArr.count>5?220:44*self.cancelResonArr.count;
    choseMenu.displaySize=CGSizeMake(cell.cancelReson.width,height);
    choseMenu.choseArr = self.cancelResonArr;
    choseMenu.delegate = self;
    [self presentViewController:choseMenu animated:YES completion:nil];
}
#pragma mark ChoseMenuViewControllerDelegate
-(void)MenuDidClickIndex:(NSInteger)index{
    ParameterModel *model=self.cancelResonArr[index];
    _cancelCode=model.code;
    [self.proxyModel setRepairBreakReason:model.code];
    [self.presentTable reloadData];
    
}
@end
